import { Army } from "./Army";
import { Building } from "./Buildings";
import { totoalArmyInfo } from "./totoalArmyInfo";

/**
 * 攻击对方
 */
export class reportInfo_Attack {
    /**
     * 攻击方军队
     */
    attackArmy: Army;
    /**
     * 损失的攻击方军队
     */
    lostAttackArmy: Army;
    /**
     * 缴获物
     */
    wood: number;
    mud: number;
    iron: number;
    food: number;
    src0: Building;
    dst0: Building;//城墙
    src1: Building;
    dst1: Building;//投石车攻击目标1
    src2: Building;
    dst2: Building;//投石车攻击目标2
    /**
     * 防守方军队
     */
    defenseArmy: Army;
    /**
     * 损失的防守方军队
     */
    lostDefenseArmy: Army;
    /**
     * 协防部队
     */
    helpArmy: Army[] = [];
    /**
     * 损失的协防部队
     */
    lostHelpArmy: Army[] = [];
    /**
     * 汇总的协防军队 totoalArmyInfo
     */
    totalHelpArmy: totoalArmyInfo;
    /**
     * 汇总的协防军队损失 totoalArmyInfo
     */
    totalLostHelpArmy: totoalArmyInfo;

    /**
     * 构造函数
     * @param {*} report 
     * @param {*} rp 
     */
    constructor(rp) {
        /* 攻击报告结构：攻击士兵串|抢劫木,抢劫泥,抢劫铁,抢劫粮|攻击损失串|建筑1,建筑1级别;建筑1,最终级别;建筑2,建筑2级别;建筑2,最终级别;建筑3,建筑3级别;建筑3,最终级别@防御士兵串|防御损失串[@协防士兵|协防损失[*协防士兵|协防损失]] */
        this.src0 = new Building();
        this.dst0 = new Building();
        this.src1 = new Building();
        this.dst1 = new Building();
        this.src2 = new Building();
        this.dst2 = new Building();

        let pList = rp.rParam.trim().split('@');
        if (pList.length >= 1) {
            //pList[0],攻击方信息，包括攻击部队、缴获物、损失部队、砸毁的建筑
            let attackInfo = pList[0].split('|');

            this.attackArmy = Army.createArmyBySoldiersString(attackInfo[0])

            this.wood = 0; this.mud = 0; this.iron = 0; this.food = 0;

            let resList = attackInfo[1].trim().split(',');
            if (resList.length == 4) {
                this.wood = parseInt(resList[0]);
                this.mud = parseInt(resList[1]);
                this.iron = parseInt(resList[2]);
                this.food = parseInt(resList[3]);
            }

            this.lostAttackArmy = Army.createArmyBySoldiersString(attackInfo[2])


            if (attackInfo.length >= 4) {
                let buildingList = attackInfo[3].trim().split(';');
                if (buildingList.length == 6) {
                    this.src0 = Building.createByIdLevelStr(buildingList[0])
                    this.dst0 = Building.createByIdLevelStr(buildingList[1])
                    // this.src0.buildingString = buildingList[0];
                    // this.dst0.buildingString = buildingList[1];
                    this.src1 = Building.createByIdLevelStr(buildingList[2])
                    this.dst1 = Building.createByIdLevelStr(buildingList[3])
                    // this.src1.buildingString = buildingList[2];
                    // this.dst1.buildingString = buildingList[3];
                    this.src2 = Building.createByIdLevelStr(buildingList[4])
                    this.dst2 = Building.createByIdLevelStr(buildingList[5])
                    // this.src2.buildingString = buildingList[4];
                    // this.dst2.buildingString = buildingList[5];
                }
            }
        }

        if (pList.length >= 2) {
            //pList[1],防守方信息，包括防守部队、损失部队

            let defenseInfo = pList[1].split('|');

            this.defenseArmy = Army.createArmyBySoldiersString(defenseInfo[0])
            this.lostDefenseArmy = Army.createArmyBySoldiersString(defenseInfo[1])


        }
        if (pList.length >= 3) {
            //pList[2],协防方信息，包括协防部队、损失部队
            let helpInfo = pList[2].split('*');
            for (let i = 0; i < helpInfo.length; i++) {
                let detail = helpInfo[i].split('|');
                let tmpArmy = Army.createArmyBySoldiersString(detail[0])
                // tmpArmy.soldiersString = detail[0];
                this.helpArmy.push(tmpArmy);
                tmpArmy = Army.createArmyBySoldiersString(detail[1])
                // tmpArmy.soldiersString = detail[1];
                this.lostHelpArmy.push(tmpArmy);
            }
        }

        this.totalHelpArmy = new totoalArmyInfo();
        this.totalLostHelpArmy = new totoalArmyInfo();
        for (let j = 0; j < this.helpArmy.length; j++) {
            let a = this.helpArmy[j]
            for (let i = 0; i < a.soldierList.length; i++) {
                let so = a.soldierList[i]
                this.totalHelpArmy.sList[so.id - 1].count += so.count
            }

        }
        for (let j = 0; j < this.lostHelpArmy.length; j++) {
            let a = this.lostHelpArmy[j]
            for (let i = 0; i < a.soldierList.length; i++) {
                let so = a.soldierList[i]
                this.totalLostHelpArmy.sList[so.id - 1].count += so.count
    
            }

        }
    }
}